package com.qc31.comlib.alarm

import android.annotation.SuppressLint
import com.qc31.baselib.utils.MLog
import com.qc31.comlib.entity.AlarmType
import com.qc31.comlib.net.ServiceHelper

/**
 *@Author Jasper
 *@Time 2021/11/20 17:12
 *@Desc
 */
class AlarmDataUtil {
    companion object {
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            AlarmDataUtil()
        }
    }

    fun init() {
        getNormalAlarm()
        getAdasAlarm()
    }

    //报警类型
    private val normalAlarms = ArrayList<AlarmType>()

    //主动安全报警类型
    private val adasAlarms = ArrayList<AlarmType>()

    @SuppressLint("CheckResult")
    fun getNormalAlarm() {
        ServiceHelper.getRepository().getAlarmType(false).subscribe({
            if (it.isNullOrEmpty()) return@subscribe
            normalAlarms.clear()
            normalAlarms.addAll(it)
        }, {
            MLog.e("获取报警类型数据失败")
        })
    }

    @SuppressLint("CheckResult")
    fun getAdasAlarm() {
        ServiceHelper.getRepository().getAlarmType(true).subscribe({
            if (it.isNullOrEmpty()) return@subscribe
            adasAlarms.clear()
            adasAlarms.addAll(it)
        }, {
            MLog.e("获取主动安全报警类型数据失败")
        })
    }

    /**
     * 通过列表获取数据
     */
    fun getAlarmIds(alarms: MutableList<AlarmType>?, isAdas: Boolean): String {
        if (alarms.isNullOrEmpty()) return ""
        var ids = ""
        var num = 0
        alarms.forEach {
            it.children.forEach { child ->
                if (child.isChoose) {
                    ids = "$ids,${child.id}"
                    num++
                }
            }
        }
        if (isAdas) adasChoose = num else normalChoose = num
        if (ids.isNotEmpty()) {
            return ids.substring(1)
        }
        return ""
    }


    /**
     * 获取默认数据
     */
    private fun getDefaultAlarm(isAdas: Boolean): ArrayList<AlarmType> {
        var size = 0
        val arrayList: ArrayList<AlarmType> =
            if (isAdas) adasAlarms.onEach { alarm ->
                size = setAlarmChoose(alarm, size, false)
            } else normalAlarms.onEach { alarm ->
                size = setAlarmChoose(alarm, size, false)
            }
        if (isAdas) adasSize = size else normalSize = size
        return arrayList

    }

    //报警类型总数
    private var normalSize = 0

    //主动安全报警类型总数
    private var adasSize = 0

    //主动安全选中数量
    private var adasChoose = 0

    //报警选中数量
    private var normalChoose = 0

    /**
     * 获取报警类型总数
     */
    fun getTotal(isAdas: Boolean) = if (isAdas) adasSize else normalSize
    fun getChooseTotal(isAdas: Boolean) = if (isAdas) adasChoose else normalChoose


    /**
     * 通过ID设置状态并返回集合
     */
    fun getAlarmByIds(isAdas: Boolean, ids: String): ArrayList<AlarmType> {
        val alarms = ArrayList<AlarmType>()
        alarms.addAll(getDefaultAlarm(isAdas))
        MLog.e("报警类型 $ids  $alarms   ")
        if (ids.isEmpty()) return alarms
        ids.split(",").forEach { id ->
            alarms.forEach { alarm ->
                alarm.children.forEach { child ->
                    if (child.id == id) {
                        child.isChoose = true
                    }
                }
            }
        }
        alarms.forEach { alarm ->
            var length = 0
            alarm.children.forEach { child ->
                if (child.isChoose) {
                    length += 1
                }
            }
            MLog.e("length= $length  size= ${alarm.children.size}")
            alarm.isAllChoose = length == alarm.children.size
        }
        return alarms
    }

    /**
     * 获取默认数据
     */
    fun getCarDefaultAlarm(isAdas: Boolean): ArrayList<AlarmType> {
        var size = 0
        val arrayList: ArrayList<AlarmType> =
            if (isAdas) adasAlarms.onEach { alarm ->
                size = if (alarm.id == "adas7Alarm" || alarm.id == "adas8Alarm") {
                    setAlarmChoose(alarm, size, true)
                } else {
                    setAlarmChoose(alarm, size, false)
                }
            } else normalAlarms.onEach { alarm ->
                size = if (alarm.id == "importantAlarm" || alarm.id == "adas7Alarm") {
                    setAlarmChoose(alarm, size, true)
                } else {
                    setAlarmChoose(alarm, size, false)
                }

            }
        if (isAdas) {
            ReportSetModel.instance.setAdasSize(size)
            ReportSetModel.instance.setAdasAlarmIds(getAlarmIds(arrayList, true))
        } else {
            ReportSetModel.instance.setNormalSize(size)
            ReportSetModel.instance.setNormalAlarmIds(getAlarmIds(arrayList, false))
        }
        return arrayList
    }

    /**
     * 设置选中状态
     */
    private fun setAlarmChoose(
        alarm: AlarmType,
        size: Int, isChoose: Boolean
    ): Int {
        var size1 = size
        alarm.isAllChoose = isChoose
        alarm.children.forEach {
            size1++
            it.isChoose = isChoose
        }
        return size1
    }
}